//
//  FKTeamAwardVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/14.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKTeamAwardVC.h"
#import "FKTeamAwardCell.h"
#import "FKTeamAward.h"

@interface FKTeamAwardVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, strong) NSMutableDictionary *param;
@property(nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UIView *emptyView;

@end

@implementation FKTeamAwardVC


- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (NSMutableDictionary *)param
{
    if (!_param) {
        _param = [NSMutableDictionary dictionary];
        _param[@"pagesize"] = @(10);
    }
    return _param;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"团队奖励";
    if ([self.type isEqualToString:@"40"]) {
        self.title = @"培训津贴";
    }
    self.param[@"type"] = self.type;
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, fk_adjustW(40))];
    [self.view addSubview:topView];
    topView.backgroundColor = fkMainColor;
    
    NSArray *titles = @[@"用户", @"上级", @"金额"];
    for (NSInteger index = 0; index < titles.count; index++) {
        UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(15) textColor:[UIColor whiteColor] text:titles[index]];
        [topView addSubview:titleLabel];
        
        if (index == 1) {
            titleLabel.textAlignment = NSTextAlignmentCenter;
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.centerY.offset(0);
            }];
        } else if (index == 2) {
            titleLabel.textAlignment = NSTextAlignmentRight;
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.offset(-12);
                make.centerY.offset(0);
            }];
        } else {
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(12);
                make.centerY.offset(0);
            }];
        }
    }
    
    UITableView *tableView = [[UITableView alloc] init];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.tableFooterView = [UIView new];
    tableView.separatorStyle= UITableViewCellSeparatorStyleNone;
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.top.equalTo(topView.mas_bottom);
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKTeamAwardCell *cell = [FKTeamAwardCell cellWith:tableView];
    cell.data = self.datas[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(55);
}

- (UIView *)emptyView
{
    if (!_emptyView) {
        _emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, 200)];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"empty_detail"]];
        [_emptyView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.offset(0);
            make.width.mas_equalTo(fk_adjustW(imageView.image.size.width));
            make.height.mas_equalTo(fk_adjustW(imageView.image.size.height));
        }];
        
        UILabel *label = [[UILabel alloc] init];
        [_emptyView addSubview:label];
        label.font = fk_adjustFont(16);
        label.text = @"您暂时还没有奖励哦~";
        label.textColor = fkColor999999;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(imageView.mas_bottom).offset(20);
            make.centerX.offset(0);
        }];
    }
    return _emptyView;
}



#pragma mark - 网络请求
- (void)loadData
{
    self.page = 1;
    self.param[@"page"] = @(self.page);
    [FKHttpTool postWithSuffix:@"m=Home&c=Member&a=porfitDetailList" param:self.param success:^(FKBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        if (result.isSuccess) {
            self.datas = [FKTeamAward mj_objectArrayWithKeyValuesArray:result.data[@"list"]];
            [self.tableView reloadData];
            if (self.datas.count > 0) self.page++;
        }
        [self.tableView fk_tableViewCheckEmptyDataWithDataCount:self.datas.count emptyView:self.emptyView];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView fk_tableViewCheckEmptyDataWithDataCount:self.datas.count emptyView:self.emptyView];
    }];
}

- (void)loadMoreData
{
    self.param[@"page"] = @(self.page);
    [FKHttpTool postWithSuffix:@"m=Home&c=Member&a=porfitDetailList" param:self.param success:^(FKBaseResult *result) {
        [self.tableView.mj_footer endRefreshing];
        if (result.isSuccess) {
            NSArray *newArray = [FKTeamAward mj_objectArrayWithKeyValuesArray:result.data[@"list"]];
            if (newArray.count > 0) {
                NSMutableArray *array = [NSMutableArray arrayWithArray:self.datas];
                [array addObjectsFromArray:newArray];
                self.datas = array;
                [self.tableView reloadData];
                self.page++;
            } else {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}


@end
